#coding:utf-8
from flask import Flask
from flask import render_template
from flask import url_for
from flask import request
import cgi
import dmcore
app = Flask(__name__)

@app.route('/play/')
@app.route('/get/')
@app.route('/getcode/')
@app.route('/')
def index():
    videos = dmcore.getAllVideo()
    return render_template('index.html',videos = videos)
@app.route('/admin/')
def admin_page():
    return 'this is admin page'
@app.route('/play/<av>')
def play(av):
    playerUrl = url_for('static',filename='MukioPlayerPlus.swf')
    videourl = dmcore.getVideoUrl(av)
    return render_template('player.html',player = playerUrl ,video =videourl,vid = av)

@app.route('/get/<id>')
def get_danmaku(id):
    xml = dmcore.get_danmaku(id)
    return dmcore.show_danmaku(xml)
@app.route('/post/',methods=['POST'])
@app.route('/post/<id>',methods=['POST'])
def post_danmu(id):
    message = request.form['message']
    mode = request.form['mode']
    stime = request.form['stime']
    size = request.form['size']
    color = request.form['color']
    return dmcore.send_danmu(message,mode,stime,size,color,'test',id)
@app.route('/getcode/<vid>')
def getcode(vid):
    videourl = dmcore.getVideoUrl(vid)
    url = request.url_root[0:-1]
    playerUrl = url + url_for('static',filename='MukioPlayerPlus.swf')
    code = ''
    code += '<embed id="MukioPlayer"\n'
    code += 'src="'+playerUrl+'?vid='+vid+'&type=video&sort=new" type="application/x-shockwave-flash"\n'
    code+='allowscriptaccess="always"\n'
    code+='quality="high"\n'
    code+='allowfullscreen="true"\n'
    code+='flashvars="file='+ videourl +'"\n'
    code+='style="width: 600;height: 450">\n'
    return cgi.escape(code)

@app.route('/add/',methods=['POST'])
def TaddVideo():
    url = request.form['url']
    if len(url)==0:
        return 'url长度为0，请检查'
    else:
        id = dmcore.addVideo(url)
        return '<a href='+ '/play/'+id+'>lookIt</a>'
@app.route('/static/conf.xml')
def renderConfig():
    url = request.url_root
    return render_template('conf.xml',url=url)
if __name__ =='__main__':
    app.run()